<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '150px' : '110px'"
    >
      <el-form-item label="楼盘名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="楼盘名称"></el-input>
      </el-form-item>
      <el-form-item label="楼盘别名" prop="anotherName">
        <el-input v-model="dataForm.anotherName" placeholder="楼盘别名"></el-input>
      </el-form-item>
      <el-form-item label="楼盘类型" prop="type">
        <ren-select
          v-model="dataForm.type"
          dict-type="dictType"
          placeholder="物业类型"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="区县" prop="districtCode">
        <el-select
          v-model="dataForm.districtCode"
          placeholder="请选择"
          style="width:100%"
          @change="CityRegionChange"
        >
          <el-option
            v-for="item in this.$store.state.CityRegion"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属商圈" prop="business">
        <el-select v-model="dataForm.business" placeholder="请选择" style="width:100%">
          <el-option
            v-for="item in businessList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="经纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="经纬度" @focus="getLngLat"></el-input>
      </el-form-item>
      <el-form-item label="最小面积" prop="areas">
        <el-input v-model="dataForm.areas" placeholder="最小面积"></el-input>
      </el-form-item>
      <el-form-item label="最大面积" prop="areae">
        <el-input v-model="dataForm.areae" placeholder="最大面积"></el-input>
      </el-form-item>

      <el-form-item label="最小出租价格">
        <el-input v-model="dataForm.leasePrices" placeholder="最小出租价格"></el-input>
      </el-form-item>

      <el-form-item label="最大出租价格">
        <el-input v-model="dataForm.leasePricee" placeholder="最大出租价格"></el-input>
      </el-form-item>

      <el-form-item label="最小出售价格">
        <el-input v-model="dataForm.sellPrices" placeholder="最小出售价格"></el-input>
      </el-form-item>

      <el-form-item label="最大出售价格">
        <el-input v-model="dataForm.sellPricee" placeholder="最大出售价格"></el-input>
      </el-form-item>

      <el-form-item label="项目描述" prop="content">
        <el-input
          v-model="dataForm.content"
          placeholder="项目描述"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>-->

      <!-- <el-form-item
        label="维护人部门"
        prop="maintain"
      >
        <el-input
          v-model="dataForm.deptName"
          placeholder="维护人部门"
          disabled
        ></el-input>
      </el-form-item>-->
      <!-- <el-form-item
        label="维护人"
        prop="maintain"
      >
        <el-select
          v-model="dataForm.maintain"
          placeholder="请选择"
          style="width:100%"
        >
          <el-option
            v-for="item in this.$store.state.maintainList"
            :key="item.id"
            :label="item.realName"
            :value="item.id"
            @click.native="maintainChange(item)"
          ></el-option>
        </el-select>
      </el-form-item>-->

      <el-form-item label="维护人部门">
        <el-cascader
          style="width:100%"
          v-model="deptSelectVal"
          :options="this.$store.state.DeptList"
          :show-all-levels="false"
          @change="deptIdChange"
          :props="deptProps"
          ref="myCascader"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="维护人">
        <el-select
          v-model="dataForm.maintain"
          placeholder="请选择"
          style="width:100%"
          :disabled="dataForm.deptId==''"
        >
          <el-option
            v-for="item in maintainList"
            :key="item.id"
            :label="item.realName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="城市租户编码" prop="tenantCode">
          <el-input v-model="dataForm.tenantCode" placeholder="城市租户编码"></el-input>
      </el-form-item>
              <el-form-item label="修改人" prop="updater">
          <el-input v-model="dataForm.updater" placeholder="修改人"></el-input>
      </el-form-item>
          <el-form-item label="修改时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false;dataForm.deptName=''">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
    <baiduMap v-if="baiduMap_dialog" ref="getBaiduMap" @getLngLat="LngLatData"></baiduMap>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import baiduMap from "../BMap/baiduMap";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        name: "",
        anotherName: "",
        type: "",
        districtCode: "",
        business: "",
        latitude: "",
        areas: "",
        areae: "",
        sellPrice: "",
        leasePrice: "",
        content: "",
        oLevel: "",
        projectYear: "",
        projectHeight: "",
        propertyYear: "",
        developers: "",
        deliverTime: "",
        occupyArea: "",
        architectureArea: "",
        buildSituation: "",
        wallHeight: "",
        clearHeight: "",
        storeyHeight: "",
        lowerLayer: "",
        upperLayer: "",
        propertyCompany: "",
        propertyPrice: "",
        propertyAdress: "",
        propertyPhone: "",
        developersUser: "",
        developersPhone: "",
        liftBrand: "",
        liftElevator: "",
        liftCargo: "",
        place: "",
        placePrice: "",
        power: "",
        water: "",
        airConditioner: "",
        airConditionerTime: "",
        remark: "",
        maintain: "",
        tenantCode: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
        deptId: "",
        deptName: ""
      },
      businessList: this.$store.state.BusinessPage,
      baiduMap_dialog: false,
      deptSelectVal: "",
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      maintainList: this.$store.state.maintainList
    };
  },
  components: {
    baiduMap
  },
  computed: {
    dataRule() {
      return {
        name: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        anotherName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        type: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        districtCode: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],

        maintain: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ]
      };
    }
  },
  created() {},
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/dictionary/dictinfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },

    //行政区选择change
    CityRegionChange(val) {
      this.$http
        .get(`/dictionary/business/businessList`, {
          params: {
            districtCode: val
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.businessList = res.data;
        })
        .catch(() => {});
    },
    // //维护人修改
    // maintainChange(item) {
    //     this.dataForm.deptId = item.deptId;
    //     this.dataForm.deptName = item.deptName;
    // },
    //点击获取经纬度
    getLngLat() {
      this.baiduMap_dialog = true;
      this.$nextTick(() => {
        this.$refs.getBaiduMap.init();
      });
    },
    //获取经纬度
    LngLatData(obj) {
      this.dataForm.latitude = obj.lng + "," + obj.lat;
    },
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.dataForm.deptId = a;
        this.getUserList(a);
      } else {
        this.dataForm.deptId = "";
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    // //维护人修改
    // maintainChange(item) {
    //     let _this = this;
    //     this.$http
    //         .put(
    //             `/dictionary/dictinfo/updateMaintain`,
    //             {},
    //             {
    //                 params: {
    //                     id: _this.floorID,
    //                     maintain: item.id,
    //                     deptId: item.deptId
    //                 }
    //             }
    //         )
    //         .then(({ data: res }) => {
    //             if (res.code !== 0) {
    //                 return this.$message.error(res.msg);
    //             }
    //             this.$message.success("更改成功");
    //             this.getInfo();
    //         })
    //         .catch(() => {});
    // },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/dictionary/dictinfo/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
